// const CryptoJS = require('crypto-js');  //引用AES源码js
import CryptoJS from "crypto-js";  // AES对称加密模块
import crypto from "crypto";  // AES对称加密模块
import aes from '@/aes_key.js'
import { Buffer } from 'buffer';
console.log(aes);
    
const key = CryptoJS.enc.Utf8.parse(aes.aes_key);  //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse(aes.aes_iv);   //十六位十六进制数作为密钥偏移量

let key_2 = aes.aes_key
let iv_2 = 'aaaaaaaaaaaaaaaa'
console.log(crypto.getCiphers());
const cipher = crypto.createCipheriv('aes-256-cbc', key_2, iv_2)
let strr = cipher.update('123123','utf-8','hex')
strr  += cipher.final('hex')
console.log(strr);

//解密方法
function Decrypt(word) {
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

//加密方法
function Encrypt(word) {
    let srcs = CryptoJS.enc.Utf8.parse(word);
    console.log(srcs,"aaaaa");
    let encrypted = CryptoJS.AES.encrypt("123123", key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    // return encrypted.ciphertext.toString().toUpperCase();
    console.log(encrypted.ciphertext.toString());
    return encrypted.ciphertext.toString()
}

export default {
    Decrypt ,
    Encrypt
}